#include <unistd.h>
#include <signal.h>
#include <stdio.h>

void FunAlarmHandler( int iSignalNo )
{
    printf( "Signal# %d received\n", iSignalNo );
}

int main(int argc, char const *argv[])
{
    signal( SIGALRM, FunAlarmHandler );

    unsigned int iAlmRet;
    iAlmRet = alarm( 4 );
    printf( "1st alarm( 4) return: %d\n", iAlmRet );

    sleep( 1 );
    iAlmRet = alarm( 10 );
    printf( "2nd alarm(10) return: %d\n", iAlmRet );

    int iCnt = 0;
    while ( 1 )
    {
        sleep( 1 );
        iCnt++;
        printf( "Running %d sec\n", iCnt );
    }

    return 0;
}



/*
功能:
    alarm函数在设定秒数结束之后, 发给自己进程一个 SIGALRM 信号.
    进程收到 SIGALRM 之后, 默认进程退出, 可以重定向信号处理函数. 
    alarm函数连续调用时, 返回上次调用alarm函数剩余的秒数

函数:
    signal
    alarm

*/